#ifndef __TwRayTerrainQuery_H__
#define __TwRayTerrainQuery_H__

#include "TwRay.h"
#include "TwTerrain.h"
#include "TwD3DHeaders.h"
#include <vector>

struct TwRayTerrainQueryResultEntry
{
	// Distance along the ray
	float distance;
	// Single intersection point
	D3DXVECTOR3 singleIntersection;
	// Comparison operator for sorting
	bool operator < (const TwRayTerrainQueryResultEntry& rhs) const
	{
		return this->distance < rhs.distance;
	}
};
typedef std::vector<TwRayTerrainQueryResultEntry> TwRayTerrainQueryResult;

class TwRayTerrainQuery
{
public:
	TwRayTerrainQuery(TwTerrain* terrain);
	virtual ~TwRayTerrainQuery();

	virtual void setRay(const TwRay& ray);
	virtual const TwRay& getRay() const;
	virtual void setSortByDistance(bool sort, unsigned short maxresults = 0);
	virtual bool getSortByDistance() const;
	virtual unsigned short getMaxResults() const;

	virtual TwRayTerrainQueryResult& execute();
	virtual TwRayTerrainQueryResult& getLastResults();
	virtual void clearResults();
	virtual bool queryResult(const D3DXVECTOR3& intersection, float distance);

protected:
	TwRay					mRay;
	bool					mSortByDistance;
	unsigned short			mMaxResults;
	TwTerrain*				mParentTerrain;
	TwRayTerrainQueryResult	mResult;
};

#endif //__TwRayTerrainQuery_H__